using System;
using System.Collections;
using System.Windows.Forms;
namespace SS
{
	public class ListViewColumnSorter : IComparer
	{
		private int ColumnToSort;
		private SortOrder OrderOfSort;
		private CaseInsensitiveComparer ObjectCompare;
		private int TypeToSort = 0;
		public int SortColumn
		{
			get
			{
				return this.ColumnToSort;
			}
			set
			{
				this.ColumnToSort = value;
			}
		}
		public SortOrder Order
		{
			get
			{
				return this.OrderOfSort;
			}
			set
			{
				this.OrderOfSort = value;
			}
		}
		public int TTSort
		{
			get
			{
				return this.TypeToSort;
			}
			set
			{
				this.TypeToSort = value;
			}
		}
		public ListViewColumnSorter()
		{
			this.ColumnToSort = 0;
			this.OrderOfSort = SortOrder.None;
			this.ObjectCompare = new CaseInsensitiveComparer();
		}
		public int Compare(object x, object y)
		{
			double num = 0.0;
			double num2 = 0.0;
			ListViewItem listViewItem = (ListViewItem)x;
			ListViewItem listViewItem2 = (ListViewItem)y;
			int num3;
			switch (this.TypeToSort)
			{
			case 0:
				num3 = this.ObjectCompare.Compare(listViewItem.SubItems[this.ColumnToSort].Text, listViewItem2.SubItems[this.ColumnToSort].Text);
				if (this.OrderOfSort != SortOrder.Ascending)
				{
					if (this.OrderOfSort == SortOrder.Descending)
					{
						num3 = -num3;
					}
					else
					{
						num3 = 0;
					}
				}
				break;
			case 1:
				try
				{
					num = double.Parse(listViewItem.SubItems[this.ColumnToSort].Text.Replace(",", "").Replace(".", ""));
				}
				catch
				{
					num = 0.0;
				}
				try
				{
					num2 = double.Parse(listViewItem2.SubItems[this.ColumnToSort].Text.Replace(",", "").Replace(".", ""));
				}
				catch
				{
					num2 = 0.0;
				}
				if (num == num2)
				{
					num3 = 0;
				}
				else
				{
					if (num > num2)
					{
						if (this.OrderOfSort == SortOrder.Ascending)
						{
							num3 = 1;
						}
						else
						{
							if (this.OrderOfSort == SortOrder.Descending)
							{
								num3 = -1;
							}
							else
							{
								num3 = 0;
							}
						}
					}
					else
					{
						if (this.OrderOfSort == SortOrder.Ascending)
						{
							num3 = -1;
						}
						else
						{
							if (this.OrderOfSort == SortOrder.Descending)
							{
								num3 = 1;
							}
							else
							{
								num3 = 0;
							}
						}
					}
				}
				break;
			case 2:
				try
				{
					DateTime t = DateTime.Parse(this.DateFormating(listViewItem.SubItems[this.ColumnToSort].Text));
					DateTime t2 = DateTime.Parse(this.DateFormating(listViewItem2.SubItems[this.ColumnToSort].Text));
					num3 = DateTime.Compare(t, t2);
				}
				catch
				{
					num3 = this.ObjectCompare.Compare(listViewItem.SubItems[this.ColumnToSort].Text, listViewItem2.SubItems[this.ColumnToSort].Text);
				}
				if (this.OrderOfSort != SortOrder.Ascending)
				{
					if (this.OrderOfSort == SortOrder.Descending)
					{
						num3 = -num3;
					}
					else
					{
						num3 = 0;
					}
				}
				break;
			default:
				num3 = this.ObjectCompare.Compare(listViewItem.SubItems[this.ColumnToSort].Text, listViewItem2.SubItems[this.ColumnToSort].Text);
				break;
			}
			return num3;
		}
		public string DateFormating(string InputStr)
		{
			string result;
			try
			{
				DateTime.Parse("12/15/2009");
				string text;
				if (InputStr.Substring(1, 1) == "/" && InputStr.Substring(3, 1) == "/")
				{
					text = string.Concat(new string[]
					{
						"0",
						InputStr.Substring(2, 1),
						"/0",
						InputStr.Substring(0, 1),
						"/",
						InputStr.Substring(4, 4)
					});
				}
				else
				{
					if (InputStr.Substring(2, 1) == "/" && InputStr.Substring(4, 1) == "/")
					{
						text = string.Concat(new string[]
						{
							"0",
							InputStr.Substring(3, 1),
							"/",
							InputStr.Substring(0, 2),
							"/",
							InputStr.Substring(5, 4)
						});
					}
					else
					{
						if (InputStr.Substring(1, 1) == "/" && InputStr.Substring(4, 1) == "/")
						{
							text = string.Concat(new string[]
							{
								InputStr.Substring(2, 2),
								"/0",
								InputStr.Substring(0, 1),
								"/",
								InputStr.Substring(5, 4)
							});
						}
						else
						{
							if (InputStr.Substring(2, 1) == "/" && InputStr.Substring(5, 1) == "/")
							{
								text = string.Concat(new string[]
								{
									InputStr.Substring(3, 2),
									"/",
									InputStr.Substring(0, 2),
									"/",
									InputStr.Substring(6, 4)
								});
							}
							else
							{
								text = InputStr;
							}
						}
					}
				}
				result = text;
			}
			catch
			{
				result = DateTime.Parse(InputStr).ToShortDateString();
			}
			return result;
		}
	}
}
